package room;

import global.IsLogin;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import model.Bussiness;
import model.Customer;
import model.Room;
import model.Roomtype;
import model.Vip;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

@Before(IsLogin.class)
public class RoomController extends Controller {

	@Before(roomITP.class)
	public void index(){
		List<Room> roomlist =Room.dao.find("select a.roomnumber,a.roomnote,b.note,b.money,a.roomID,a.roomstate " +
				"from room a,roomtype b where a.roomtype=b.roomtype");
		String msgbox=getPara("msgbox");
		String a = null;
		if(msgbox!=null&&!msgbox.isEmpty()&&!msgbox.equals("")){
			if(msgbox.equals("1")){
				a="客户信息存在";
			}else if(msgbox.equals("1")){
				a="客户信息没有";
			}else if(msgbox.equals("1")){
				a="成功入住";
			}
			setAttr("msgbox", a);
		}
		setAttr("roomlist", roomlist);
		render("/room/room.html");
	}
	public void gotourl(){
		String page=getPara("page");
		if(page.equals("add")){
			setAttr("action", "add");
			setAttr("roomtypes", getType());
			render("/room/roomupdate.html");
		}else if(page.equals("update")){
			String roomid=getPara("id");
			Room aroom=Room.dao.findById(roomid);
			setAttr("action", "update");
			setAttr("room", aroom);
			setAttr("roomtypes", getType());
			/*Map<String, Object> imp=new HashMap<String, Object>();
			imp.put("action", "update");
			imp.put("room", aroom);*/
			render("/room/roomupdate.html");
		}else if(page.equals("type")){
			List<Roomtype> roomtype=Roomtype.dao.find("select * from roomtype");
			setAttr("roomtypelist", roomtype);
			render("/room/roomtype.html");
		}else if(page.equals("bussiness")){
			String roomid=getPara("roomid");
			Room room =Room.dao.findById(roomid);
			setAttr("room", room);
			render("/room/roomuse.html");			
		}else if(page.equals("use")){
			setAttr("roomnumber", getPara("roomnumber"));
			render("/room/roomuse.html");
		}else if(page.equals("sel")){
			render("/room/selcustomer.html");
		}else{
			redirect("/room/gotourl?page=type", true);
		}
		
	}	
	public void delete(){
		String id=getPara("id");
		Room room=Room.dao.findById(id);
		room.delete();
		redirect("/room");
	}
	public void add(){
		String roomnumber=getPara("roomnumber");
		String roomnote=getPara("roomnote");
		String roomtype=getPara("roomtype");
		new Room().set("roomnumber", roomnumber).set("roomnote", roomnote).set("roomtype", roomtype).save();
		redirect("/room",true);
	}
	public void update(){
		String roomnumber=getPara("roomnumber");
		String roomnote= getPara("roomnote");
		String roomtype=getPara("roomtype");
		String roomid=getPara("roomid");
		Room room=Room.dao.findById(roomid);
		room.set("roomnumber", roomnumber).set("roomnote", roomnote).set("roomtype", roomtype).update();
		redirect("/room");
	}
	public void typedit(){
		String roomtype=getPara("roomtype");
		String note=getPara("note");
		String money=getPara("money");
		if(roomtype.equals("add")){
			new Roomtype().set("note", note).set("money", money).save();
		}
		else{
			Roomtype roomt=Roomtype.dao.findById(roomtype);
			if(!note.equals("")){
				roomt.set("note", note);
			}
			if(!money.equals("")){
				roomt.set("money", money);
			}
			roomt.update();
		}
		redirect("/room/gotourl?page=type");
	}
	public void roomuse(){
		 String roomnumber=getPara("roomnumber");
		 String customername=getPara("customername");
		 String customercredit=getPara("customercredit");
		 int isvip=getParaToInt("isvip");//1入住过，2没有住过
	

				Room room = Room.dao.findFirst(
						"select * from room where roomnumber=?", roomnumber);
							System.out.println(roomnumber);
				if (isvip == 2) {
					Customer.dao.set("customercredit", customercredit)
							.set("customername", customername).set("starlv", 1)
							.save();
				}
				Customer cust = Customer.dao
						.findFirst(
								"select * from customer where customername=? and customercredit=?",
								customername, customercredit);
	
				room.set("roomstate", 0).update();
				int type = room.getInt("roomtype");
				Roomtype roomtype = Roomtype.dao.findFirst(
						"select * from roomtype where roomtype =?", type);
				Vip vip = Vip.dao.findFirst(
						"select * from vipstar where starlv=?",
						cust.get("starlv"));
				BigDecimal pay = vip.getBigDecimal("welfare").multiply(roomtype.getBigDecimal("money"));
				pay=pay.setScale(3, BigDecimal.ROUND_HALF_UP);
				System.out.println("支付：" + pay.toString());
				
				setAttr("pay", pay);
				setAttr("room", room);
				setAttr("customer", cust);
				render("/room/roompay.html");
			
				
	}
	public void pay(){
		String pay=getPara("pay");
		String roomID=getPara("roomID");
		String customerID=getPara("customerID");
		System.out.println(roomID);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		BigDecimal bd=new BigDecimal(pay);
		java.util.Date date=new java.util.Date();
		Bussiness.dao.set("roomID", roomID).set("customerID", customerID).set("paymoney", pay).set("willback", 0).set("intime", sdf.format(new java.util.Date())).set("outtime", sdf.format(new java.util.Date())).set("usehour", 0).save();
		
		redirect("/room?msgbox=3",true);
		
	}
	public void havecustomer(){
		String name=getPara("name");
		String credit =getPara("credit");
		List<Customer> customers=Customer.dao.find("select * from customer where customername=? and customercredit=?", name,credit);
	    String ishava;
		if(!customers.isEmpty()){
	    	ishava="1";
	    }else{
	    	ishava="2";
	    }
	    redirect("/room?msgbox="+ishava,true);
	}
	public void service(){
	}
	public List<Roomtype> getType(){
		String sql="select * from roomtype";
		List<Roomtype> roomtypes=Roomtype.dao.find(sql);
		return roomtypes;
	}
}
